# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=srecord
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.65.0
pkgrel=1
pkgdesc="The SRecord package is a collection of powerful tools for manipulating EPROM load files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://srecord.sourceforge.io/"
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             groff)

source=("https://sourceforge.net/projects/srecord/files/srecord/${pkgver:0:4}/srecord-${pkgver}-Source.tar.gz"
        "0001-undef-msvcrt-config-messy.patch"
        "0003-no-pdf-docs.patch"
        "0004-manpages-mingw.patch"
        "0005-no-install-deps.patch"
        "0006-lib-name.patch")
sha256sums=('81c3d07cf15ce50441f43a82cefd0ac32767c535b5291bcc41bd2311d1337644'
            'f5f606656e77f82f11d6481fcecdcc19fc360086a1269ce34ae6497fce1056e9'
            '2a0c612f31b30707637018be29d92f7dc6f4f0736b89a097c823ff94a06c6dfb'
            '2078be995995f702f7f62e530ce705bdcd2cae8ee94949874fb468b815e3ffa4'
            'aeaa94c4b2fb0991bdbb6613f22f0482b6d45f4f3c05dd21efbdca82625014fa'
            'fadebb4cf14ef800a9cf24e27131b0ec2443f355af6af03de02dcb853599c1ef')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "$srcdir/${_realname}-${pkgver}-Source"

  apply_patch_with_msg \
    0001-undef-msvcrt-config-messy.patch \
    0003-no-pdf-docs.patch \
    0004-manpages-mingw.patch \
    0005-no-install-deps.patch \
    0006-lib-name.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #CXXFLAGS+=" -Wno-deprecated-declarations -Wno-unknown-pragmas" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_STATIC_LIBRARY_PREFIX="" \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}-Source

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}-Source/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}

